package com.xdap.init.controller;

import com.definesys.mpaas.common.http.Response;
import com.xdap.common.utils.TxtUtil;
import com.xdap.init.service.ImpExcelConService;
import com.xdap.init.service.InitControllerService;
import com.xdap.init.utils.CreateJavaFIleUtils;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.logging.Handler;

/**
 * @auth lishushu
 * @2023/4/7 11:26
 * @desc {生成controller}
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/df/createController")
@Slf4j
public class InitConController {

    @Autowired
    private InitControllerService initControllerService;
    @Autowired
    private ImpExcelConService impExcelConService;

    @SneakyThrows
    @PostMapping("/test")
    public void test1() throws IOException {
        //设置velocity资源加载器
//        Properties prop = new Properties();
//        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(CreateJavaFIleUtils.prop);

        //创建Velocity容器
//        VelocityContext context = new VelocityContext();
//        context.put("name", "zhangsan");
//        //加载模板
//        Template tpl = Velocity.getTemplate("base/demo1.vm", "UTF-8");


        // 添加参数
        VelocityContext context = new VelocityContext();
        context.put("className", "Test");
        context.put("time", "2022-03-07");
        context.put("log", "这是一个日志");
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0 ; i < 10 ; i ++){
            Map<String,Object> m = new HashMap<>();
            m.put("column","column_"+i);
            m.put("field","field_"+i);
            m.put("log","log_"+i);
            list.add(m);
        }
        context.put("items", list);



        //加载模板
        Template tpl = Velocity.getTemplate("base/vo.vm", "UTF-8");

        // 这个地方的目录，需要先行创建出来才会生效
        FileWriter fw  = new FileWriter("D:\\360安全浏览器下载\\xxx\\TestVo.java");
        //合并数据到模板
        tpl.merge(context, fw);

        //释放资源
        fw.close();
    }
    /**
    *@auth lishushu
    *@2023/4/19 14:20
    *@desc {生成对应的常用的service和controller方法}
    **/
    @PostMapping("/importExcel")
    public Response target(@RequestParam("filePath") String filePath, @RequestParam("formId") String formId,
                           @RequestParam("packageName") String packageName) {
        Response res = new Response();
        try {
            packageName = packageName.toLowerCase();
            String result = impExcelConService.init(filePath,formId,packageName);
            res.setData(result);
        } catch (Exception e) {
            log.error("初始化持久层报错:{}", e);
            res.setCode(Response.CODE_ERR);
            res.setMessage(e.getMessage());
        }
        return res;
    }


}
